package com.network.network;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Bruce on 2017/7/1.
 */

public class RequestThread implements Runnable {
    private static final String TAG = "run";
    private ResponseCallback responseCallback;
    private Handler mHandler;
    private String urlStr;
    private DataList dataList;
    private BaseRequest.RequestType requestType;

    public RequestThread(BaseRequest request) {
        this.responseCallback = request.getCallback();
        this.mHandler = request.getHandler();
        this.urlStr = request.getUrl();
        this.requestType = request.getRequestType();
        this.dataList = request.getDataList();
        if (requestType == BaseRequest.RequestType.GET && dataList != null) {
            urlStr += dataList.getAllDataForPOST();
        }
    }

    @Override
    public void run() {
        try {
            URL url = new URL(urlStr);
            Log.i(TAG, "run: "+urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            setConn(conn);
            int responseCode = conn.getResponseCode();
            Message msg = Message.obtain();
            msg.obj = responseCallback;
            if (responseCode == 200) {
                InputStream is = conn.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                msg.arg1 = 1;
                Bundle bundle = new Bundle();
                bundle.putString("result", sb.toString());
                msg.setData(bundle);
                br.close();
                isr.close();
                is.close();
            } else {
                msg.arg1 = 0;
                msg.arg2 = responseCode;
            }
            mHandler.sendMessage(msg);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setConn(HttpURLConnection conn) throws IOException {
        conn.setRequestProperty("charset", "utf-8");
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(5000);
        conn.setDoInput(true);
        if (requestType == BaseRequest.RequestType.GET) {
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Content-Type", "text/html");
            conn.connect();
        } else if (requestType == BaseRequest.RequestType.POST) {
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            os.write(dataList.getAllDataForPOST().getBytes("utf-8"));
            os.flush();
            os.close();
        }
    }
}
